package pers.sloera.leetcode.editor.cn;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 最长特殊序列 II
 * user sloera
 * date 2022-06-27 22:38:51
 */
class LongestUncommonSubsequenceIi {
    public static void main(String[] args) {
        Solution solution = new LongestUncommonSubsequenceIi().new Solution();
        System.out.println(solution.findLUSlength(new String[]{"aba", "cdc", "eae"}));
        System.out.println(solution.findLUSlength(new String[]{"aaa", "aaa", "aa"}));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int findLUSlength(String[] strs) {
            Optional<Integer> minOptional = Arrays.stream(strs).distinct().map(src -> {
                Pattern pattern = Pattern.compile(Arrays.stream(src.split("")).collect(Collectors.joining(".*", "^.*", ".*$")));
                AtomicBoolean selfBoolean = new AtomicBoolean();
                Optional<String> repeatOptional = Arrays.stream(strs).filter(s -> s.length() >= src.length()).filter(s -> {
                    if (!selfBoolean.get() && src.equals(s)) {
                        selfBoolean.set(true);
                        return false;
                    }
                    return true;
                }).filter(s -> pattern.matcher(s).find()).findAny();
                return repeatOptional.isPresent() ? -1 : src.length();
            }).max(Comparator.naturalOrder());
            return minOptional.orElse(-1);
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}
